package com.gjzhao.fammll.listener;

import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: return 机制消息监听类
 * @Author: gjzhao
 * @Date Created in 2022-02-19 6:54 下午
 */
@Component
public class RabbitmqReturnListener implements RabbitTemplate.ReturnsCallback { //return机制的监听类 需要添加实现 RabbitTemplate.ReturnsCallback

    private Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnsCallback(this);
    }

    /**
     * 监听消息return机制的处理逻辑
     * 用于处理不可路由的情况：发送消息时，当交换机不存在或指定路由key不存在时，return机制生效
     *
     * @param returnedMessage
     */
    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {

        logger.info("------路由发生异常，监听到return机制反馈的消息[return反馈机制]----");
        logger.info("-----反馈结果：{}----",returnedMessage);

    }
}
